package com.lvmama.system.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.lvmama.autoconfig.ConfigManager;
import com.lvmama.entity.system.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lvmama.entity.system.Menu;
import com.lvmama.entity.system.SysUser;
import com.lvmama.system.service.MenuService;
import com.lvmama.system.util.CookieUtil;
import com.lvmama.system.util.SerializeUtil;

import org.springframework.web.servlet.tags.form.SelectTag;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


@Controller
public class LoginController {

	@Autowired
    private MenuService menuService;
	
	@Autowired
	private JedisPool jedisPool;

	@Autowired
	private ConfigManager configManager;

	@RequestMapping(value = { "/", "/login" })
    public String login(ModelMap model, HttpServletRequest request, String service) {	
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		request.getSession().setAttribute("service", service);
		if(!(auth instanceof AnonymousAuthenticationToken)) {
			
			String token = CookieUtil.getCookieByName(request, "token").getValue();
			Jedis jedis = this.jedisPool.getResource();
			byte[] userByteArray = jedis.get(token.getBytes());
			jedis.close();
			if(null == service) {
				if(userByteArray == null) {
					return "login";
				}
				SysUser currentUser = (SysUser) SerializeUtil.deSerialize(userByteArray);
				List<Menu> menus = menuService.findShowListByUserAndSysCode(currentUser.getId(), "server");
				model.addAttribute("menus", menus);

				Set<String> sysCodeSet = new HashSet<String>();
				for(Menu menu : menus){
					if(!"1".equals(menu.getId())){
						sysCodeSet.add(menu.getSysCode());
					}
				}
				List<Config> configList = configManager.showAllConfig();
				model.addAttribute("configList", configList);

				if(sysCodeSet.contains("server")){
					model.addAttribute("isAdmin", true);
					return "index";
				}
				return "index_not_admin";
			}else {
				if(userByteArray != null) {
					return "redirect:" + service + "?token=" + token;
				}
				return "login";
			}
			
		}
		
        return "login";
    }
	@RequestMapping(value="/logout")
    public String logout(HttpServletRequest request){
		String token = CookieUtil.getCookieByName(request, "token").getValue();
	    if(token != null){
	    	Jedis jedis = this.jedisPool.getResource();
	    	jedis.del(token.getBytes());
			jedis.close();
	    }
		
        return "login";
    }
    @RequestMapping(value="/deny")
    public String handleDeny(){
       return "deny";
    }
    @RequestMapping("/index")
    public String tosso(ModelMap model,String mainFrame,HttpServletRequest request) {
    	String token = CookieUtil.getCookieByName(request, "token").getValue();
		Jedis jedis = this.jedisPool.getResource();
		byte[] userByteArray = jedis.get(token.getBytes());
		jedis.close();
		SysUser currentUser = null;
		if(userByteArray == null) {
			return "redirect:/login";
		}else {
			currentUser = (SysUser) SerializeUtil.deSerialize(userByteArray);
		}
		List<Menu> menus = menuService.findShowListByUserAndSysCode(currentUser.getId(), "server");
		model.addAttribute("menus", menus);
		List<Config> configList = configManager.showAllConfig();
		model.addAttribute("configList", configList);
		model.addAttribute("mainFrame", mainFrame);
		model.addAttribute("currentUser", currentUser);
		Set<String> sysCodeSet = new HashSet<String>();
		for(Menu menu : menus){
			if(!"1".equals(menu.getId())){
				sysCodeSet.add(menu.getSysCode());
			}
		}

		if(sysCodeSet.contains("server")){
			model.addAttribute("isAdmin", true);
			return "index";
		}
		return "index_not_admin";
    }
    
}